home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac Pascal Primer, 4.0 / Chap 5, Timer ƒ / Timer.p next >
Text File  |  1990-07-30  |  7KB  |  320 lines

  1. program Timer;
  2.     const
  3.         BASE_RES_ID = 400;
  4.  
  5.         PLAIN = [];
  6.         PLAIN_ITEM = 1;
  7.         BOLD_ITEM = 2;
  8.         ITALIC_ITEM = 3;
  9.         UNDERLINE_ITEM = 4;
  10.         OUTLINE_ITEM = 5;
  11.         SHADOW_ITEM = 6;
  12.  
  13.         INCLUDE_SECONDS = TRUE;
  14.  
  15.         ADD_CHECK_MARK = TRUE;
  16.         REMOVE_CHECK_MARK = FALSE;
  17.  
  18.         SLEEP = 60;
  19.         WNE_TRAP_NUM = $60;
  20.         UNIMPL_TRAP_NUM = $9F;
  21.  
  22.         QUIT_ITEM = 1;
  23.         ABOUT_ITEM = 1;
  24.  
  25.         NOT_A_NORMAL_MENU = -1;
  26.         APPLE_MENU_ID = BASE_RES_ID;
  27.         FILE_MENU_ID = BASE_RES_ID + 1;
  28.         FONT_MENU_ID = 100;
  29.         STYLE_MENU_ID = 101;
  30.  
  31.         CLOCK_LEFT = 12;
  32.         CLOCK_TOP = 25;
  33.         CLOCK_SIZE = 24;
  34.  
  35.         ABOUT_ALERT = 400;
  36.  
  37.     var
  38.         gClockWindow: WindowPtr;
  39.         gDone, gWNEImplemented: BOOLEAN;
  40.         gCurrentTime, gOldTime: LONGINT;
  41.         gTheEvent: EventRecord;
  42.         gLastFont: INTEGER;
  43.         gCurrentStyle: Style;
  44.  
  45.  
  46. {-------------------------------->    HandleStyleChoice    <---}
  47.  
  48.     procedure CheckStyles;
  49.         var
  50.             styleMenu: MenuHandle;
  51.     begin
  52.         styleMenu := GetMHandle(STYLE_MENU_ID);
  53.         CheckItem(styleMenu, PLAIN_ITEM, (gCurrentStyle = PLAIN));
  54.         CheckItem(styleMenu, BOLD_ITEM, (bold in gCurrentStyle));
  55.         CheckItem(styleMenu, ITALIC_ITEM, (italic in gCurrentStyle));
  56.         CheckItem(styleMenu, UNDERLINE_ITEM, (underline in gCurrentStyle));
  57.         CheckItem(styleMenu, OUTLINE_ITEM, (outline in gCurrentStyle));
  58.         CheckItem(styleMenu, SHADOW_ITEM, (shadow in gCurrentStyle));
  59.     end;
  60.  
  61.  
  62. {-------------------------------->    HandleStyleChoice    <---}
  63.  
  64.     procedure HandleStyleChoice (theItem: INTEGER);
  65.     begin
  66.         case theItem of
  67.             PLAIN_ITEM: 
  68.                 gCurrentStyle := PLAIN;
  69.             BOLD_ITEM: 
  70.                 if bold in gCurrentStyle then
  71.                     gCurrentStyle := gCurrentStyle - [bold]
  72.                 else
  73.                     gCurrentStyle := gCurrentStyle + [bold];
  74.             ITALIC_ITEM: 
  75.                 if italic in gCurrentStyle then
  76.                     gCurrentStyle := gCurrentStyle - [italic]
  77.                 else
  78.                     gCurrentStyle := gCurrentStyle + [italic];
  79.             UNDERLINE_ITEM: 
  80.                 if underline in gCurrentStyle then
  81.                     gCurrentStyle := gCurrentStyle - [underline]
  82.                 else
  83.                     gCurrentStyle := gCurrentStyle + [underline];
  84.             OUTLINE_ITEM: 
  85.                 if outline in gCurrentStyle then
  86.                     gCurrentStyle := gCurrentStyle - [outline]
  87.                 else
  88.                     gCurrentStyle := gCurrentStyle + [outline];
  89.             SHADOW_ITEM: 
  90.                 if shadow in gCurrentStyle then
  91.                     gCurrentStyle := gCurrentStyle - [shadow]
  92.                 else
  93.                     gCurrentStyle := gCurrentStyle + [shadow];
  94.         end;
  95.         CheckStyles;
  96.         TextFace(gCurrentStyle);
  97.     end;
  98.  
  99.  
  100. {-------------------------------->    HandleFontChoice    <---}
  101.  
  102.     procedure HandleFontChoice (theItem: INTEGER);
  103.         var
  104.             fontNumber: INTEGER;
  105.             fontName: Str255;
  106.             fontMenu: MenuHandle;
  107.     begin
  108.         fontMenu := GetMHandle(FONT_MENU_ID);
  109.         CheckItem(fontMenu, gLastFont, REMOVE_CHECK_MARK);
  110.         CheckItem(fontMenu, theItem, ADD_CHECK_MARK);
  111.         gLastFont := theItem;
  112.         GetItem(fontMenu, theItem, fontName);
  113.         GetFNum(fontName, fontNumber);
  114.         TextFont(fontNumber);
  115.     end;
  116.  
  117.  
  118. {-------------------------------->    HandleFileChoice    <---}
  119.  
  120.     procedure HandleFileChoice (theItem: INTEGER);
  121.     begin
  122.         case theItem of
  123.             QUIT_ITEM: 
  124.                 gDone := TRUE;
  125.         end;
  126.     end;
  127.  
  128.  
  129. {-------------------------------->    HandleAppleChoice    <---}
  130.  
  131.     procedure HandleAppleChoice (theItem: INTEGER);
  132.         var
  133.             accName: Str255;
  134.             accNumber, itemNumber, dummy: INTEGER;
  135.             appleMenu: MenuHandle;
  136.     begin
  137.         case theItem of
  138.             ABOUT_ITEM: 
  139.                 dummy := NoteAlert(ABOUT_ALERT, nil);
  140.             otherwise
  141.                 begin
  142.                     appleMenu := GetMHandle(APPLE_MENU_ID);
  143.                     GetItem(appleMenu, theItem, accName);
  144.                     accNumber := OpenDeskAcc(accName);
  145.                 end;
  146.         end;
  147.     end;
  148.  
  149.  
  150. {-------------------------------->    HandleMenuChoice    <---}
  151.  
  152.     procedure HandleMenuChoice (menuChoice: LONGINT);
  153.         var
  154.             theMenu, theItem: INTEGER;
  155.     begin
  156.         if menuChoice <> 0 then
  157.             begin
  158.                 theMenu := HiWord(menuChoice);
  159.                 theItem := LoWord(menuChoice);
  160.  
  161.                 case theMenu of
  162.                     APPLE_MENU_ID: 
  163.                         HandleAppleChoice(theItem);
  164.                     FILE_MENU_ID: 
  165.                         HandleFileChoice(theItem);
  166.                     FONT_MENU_ID: 
  167.                         HandleFontChoice(theItem);
  168.                     STYLE_MENU_ID: 
  169.                         HandleStyleChoice(theItem);
  170.                 end;
  171.  
  172.                 HiliteMenu(0);
  173.             end;
  174.     end;
  175.  
  176.  
  177. {-------------------------------->    HandleMouseDown    <---}
  178.  
  179.     procedure HandleMouseDown;
  180.         var
  181.             whichWindow: WindowPtr;
  182.             thePart: INTEGER;
  183.             menuChoice, windSize: LONGINT;
  184.     begin
  185.         thePart := FindWindow(gTheEvent.where, whichWindow);
  186.         case thePart of
  187.             inMenuBar: 
  188.                 begin
  189.                     menuChoice := MenuSelect(gTheEvent.where);
  190.                     HandleMenuChoice(menuChoice);
  191.                 end;
  192.             inSysWindow: 
  193.                 SystemClick(gTheEvent, whichWindow);
  194.             inDrag: 
  195.                 DragWindow(whichWindow, gTheEvent.where, screenBits.bounds);
  196.             inGoAway: 
  197.                 gDone := TRUE;
  198.         end;
  199.     end;
  200.  
  201.  
  202. {-------------------------------->    DrawClock    <---}
  203.  
  204.     procedure DrawClock (theWindow: WindowPtr);
  205.         var
  206.             myTimeString: Str255;
  207.     begin
  208.         IUTimeString(gCurrentTime, INCLUDE_SECONDS, myTimeString);
  209.         EraseRect(theWindow^.portRect);
  210.         MoveTo(CLOCK_LEFT, CLOCK_TOP);
  211.         DrawString(myTimeString);
  212.         gOldTime := gCurrentTime;
  213.     end;
  214.  
  215.  
  216. {-------------------------------->    HandleNull    <---}
  217.  
  218.     procedure HandleNull;
  219.     begin
  220.         GetDateTime(gCurrentTime);
  221.         if gCurrentTime <> gOldTime then
  222.             DrawClock(gClockWindow);
  223.     end;
  224.  
  225.  
  226. {-------------------------------->    HandleEvent    <---}
  227.  
  228.     procedure HandleEvent;
  229.         var
  230.             theChar: CHAR;
  231.             dummy: BOOLEAN;
  232.     begin
  233.         if gWNEImplemented then
  234.             dummy := WaitNextEvent(everyEvent, gTheEvent, SLEEP, nil)
  235.         else
  236.             begin
  237.                 SystemTask;
  238.                 dummy := GetNextEvent(everyEvent, gTheEvent);
  239.             end;
  240.  
  241.         case gTheEvent.what of
  242.             nullEvent: 
  243.                 HandleNull;
  244.             mouseDown: 
  245.                 HandleMouseDown;
  246.             keyDown, autoKey: 
  247.                 begin
  248.                     theChar := CHR(BitAnd(gTheEvent.message, charCodeMask));
  249.                     if (BitAnd(gTheEvent.modifiers, cmdKey) <> 0) then
  250.                         HandleMenuChoice(MenuKey(theChar));
  251.                 end;
  252.             updateEvt: 
  253.                 begin
  254.                     BeginUpdate(WindowPtr(gTheEvent.message));
  255.                     EndUpdate(WindowPtr(gTheEvent.message));
  256.                 end;
  257.         end;
  258.     end;
  259.  
  260.  
  261. {-------------------------------->    MainLoop    <---}
  262.  
  263.     procedure MainLoop;
  264.     begin
  265.         gDone := FALSE;
  266.         gWNEImplemented := (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) <> NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  267.         while (gDone = FALSE) do
  268.             HandleEvent;
  269.     end;
  270.  
  271.  
  272. {-------------------------------->    MenuBarInit    <---}
  273.  
  274.     procedure MenuBarInit;
  275.         var
  276.             myMenuBar: Handle;
  277.             aMenu: MenuHandle;
  278.     begin
  279.         myMenuBar := GetNewMBar(BASE_RES_ID);
  280.         SetMenuBar(myMenuBar);
  281.         DisposHandle(myMenuBar);
  282.  
  283.         aMenu := GetMHandle(APPLE_MENU_ID);
  284.         AddResMenu(aMenu, 'DRVR');
  285.  
  286.         aMenu := GetMenu(FONT_MENU_ID);
  287.         InsertMenu(aMenu, NOT_A_NORMAL_MENU);
  288.         AddResMenu(aMenu, 'FONT');
  289.  
  290.         aMenu := GetMenu(STYLE_MENU_ID);
  291.         InsertMenu(aMenu, NOT_A_NORMAL_MENU);
  292.         CheckItem(aMenu, PLAIN_ITEM, TRUE);
  293.  
  294.         DrawMenuBar;
  295.         gLastFont := 1;
  296.         gCurrentStyle := PLAIN;
  297.         HandleFontChoice(gLastFont);
  298.     end;
  299.  
  300.  
  301. {-------------------------------->    WindowInit    <---}
  302.  
  303.     procedure WindowInit;
  304.     begin
  305.         gClockWindow := GetNewWindow(BASE_RES_ID, nil, WindowPtr(-1));
  306.         SetPort(gClockWindow);
  307.         ShowWindow(gClockWindow);
  308.  
  309.         TextSize(CLOCK_SIZE);
  310.     end;
  311.  
  312.  
  313. {-------------------------------->    Timer    <---}
  314.  
  315. begin
  316.     WindowInit;
  317.     MenuBarInit;
  318.  
  319.     MainLoop;
  320. end.